package xhh.dyf;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OwHtmlPaser {

    public static void main(String[] args) throws IOException {

        String[] urls = new String[]{
                "http://www.xinhehua.com/?list_49/", // 毒性饮片
                "http://www.xinhehua.com/?lm1/", // 普通
                "http://www.xinhehua.com/?list_50/", // 动物类饮片
                "http://www.xinhehua.com/?lm3/", // 单方饮片
        };
        String imgPrefix = "http://www.xinhehua.com";

        for (String url : urls) {

            List<Object> items = new ArrayList<>();
            Document document = Jsoup.parse(new URL(url), 10_000);

            Element honorlist = document.getElementsByClass("honorlist").get(0);
            Elements lis = honorlist.child(0).children();
            for (Element li : lis) {
                Element a = li.child(0);
                Element img = a.child(0);

                String src = img.attribute("src").getValue();
                String name = a.child(2).text();

                String imgUrl = imgPrefix + src;

                Map<String, Object> eleMap = MapBuilder.<String, Object>create()
                        .put("name", name)
                        .put("url", imgUrl)
                        .build();

                items.add(eleMap);
            }

            System.out.println(JSON.toJSONString(items));

            System.out.println("----------------");
        }

    }

}
